home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 1 / ETO Development Tools 1.iso / Essentials / C++ AppleLink Messages / CPlus.Dev$ 3⁄2⁄90 / 0060-Waste in Constructor-Mar90 < prev    next >
Encoding:
Text File  |  1990-03-02  |  2.1 KB  |  83 lines  |  [TEXT/GEOL]

  1. Item    9905085                         1-March-90        15:44PST
  2.  
  3. From:   FARALLON.ENG                    Farallon, Engineering,PRT
  4.  
  5. To:     CPLUS.DEV$                      C++ Interest List--Developers
  6.  
  7. Sub:    Waste in Constructor call
  8.  
  9.  
  10. (This may seem like a trivial point, but we bring it up because it's
  11. constructors are fundamental to writing applications and show up
  12. everywhere.)
  13.  
  14. We're creating a simple object for doing fixed-point integer math.
  15. Here's some of the C++ class description:
  16.  
  17.     class TFixed
  18.     {
  19.         public:
  20.             TFixed( const int );
  21.             ~TFixed( void );
  22.             ...
  23.         private:
  24.             long fValue;
  25.     };
  26.  
  27.     inline TFixed::TFixed( const int value )
  28.     {
  29.         fValue = value << 16; // machine dependent
  30.     }
  31.  
  32.     inline TFixed::~TFixed()
  33.     {
  34.     }
  35.  
  36. Here's a very simple test driver:
  37.  
  38.     void main( void )
  39.     {
  40.         TFixed a = 1;
  41.         TFixed b(2);
  42.     }
  43.  
  44. We would estimate that the code for this would be very simple.  Unfortunately,
  45. it always seems to generate an extra instruction.  After compiling with CPlus,
  46. DumpObj produces:
  47.  
  48.     LINK       A6,#$FFF8
  49.     MOVE.L     #$00010000,-$0008(A6)
  50.     LEA        -$0008(A6),A0
  51.     MOVE.L     #$00020000,-$0004(A6)
  52.     LEA        -$0004(A6),A0
  53.     UNLK       A6
  54.     RTS
  55.  
  56. What are the LEA instructions there for?  They put a value into A0 that's never
  57. used.  Looking at the intermediate C code gives us a clue:
  58.  
  59.  
  60.     void main(void ){
  61.         {
  62.             struct TFixed a;
  63.             struct TFixed b;
  64.             ( ((& a)-> fValue= (((int )1)<< 16)), (& a)) ;
  65.             ( ((& b)-> fValue= (((int )2)<< 16)), (& b)) ;
  66.             ( (( (( (( 0) ), 0) ), 0) )) ;
  67.             ( (( (( (( 0) ), 0) ), 0) )) ;
  68.         }
  69.     }
  70.  
  71. Since a constructor always returns the address of the object it's
  72. initializing, it always gets dumped onto A0.  Is there a way to
  73. make the MPW C optimize these out?  or a way to prevent CFront from
  74. generating them for stack-based objects (that don't need a value
  75. returned)?
  76.  
  77. (It's interesting to note that the line "TFixed c = b;" doesn't generate the
  78. extra LEA.)
  79.  
  80. Anybody else noticed this?
  81.  
  82.  
  83.